package com.cxh.controller.car;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.cxh.base.BaseController;
import com.cxh.entity.CarModel;
import com.cxh.entity.CarModelOthername;
import com.cxh.util.CommonUtility;
import com.cxh.util.DateUtility;
import com.cxh.util.json.JSONUtil;

import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 类说明：学生表 
 * @author  ChenXH
 * @date  2018-10-29 16:54:27
 */
@Controller
@RequestMapping(value = "/controller/carModelOthername")
public class CarModelOthernameController extends BaseController {

    /**
     * 方法说明：进入页面
     * @author  ChenXH
     * @date  2018-10-29 16:54:27
     */
    @RequestMapping(value = "/list")
    public ModelAndView list (HttpServletRequest request, HttpServletResponse response){
        ModelAndView model =  new ModelAndView();
        model.setViewName("car/carModelOthername/carModelOthernameList");
        return model;
    }

    /**
     * 方法说明：获得表格数据
     * @author  ChenXH
     * @date  2018-10-29 16:54:27
     */
    @RequestMapping(value = "/showList")
    @ResponseBody
    public String showList (HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
        String json="";
        //获得  页码数
        Integer page=CommonUtility.isEmpty(params.get("page"))?1:Integer.parseInt(params.get("page")+"");
        //获得 每页的行数
        Integer limit=CommonUtility.isEmpty(params.get("limit"))?10:Integer.parseInt(params.get("limit")+"");
        //设置 分页参数
        PageHelper.startPage(page,limit);
        //调用 查询方法
        List<CarModelOthername> carModelOthernameList=carModelOthernameService.loadPageList(params);
        //处理数据
        PageInfo<CarModelOthername> pageInfo=new PageInfo<CarModelOthername>(carModelOthernameList);
        try {
            //转换 时间格式/并将数据转换成json字符串
            JsonConfig jsonConfig = JSONUtil.getJsonConfig(DateUtility.DateFormat1);
            json= JSONUtil.toLayUIJSONAll(pageInfo.getTotal(),pageInfo.getList(), jsonConfig);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }


    /**
     * 方法说明：进入编辑页面
     * @author  ChenXH
     * @date  2018-10-29 16:54:27
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit (HttpServletRequest request, HttpServletResponse response){
        //获得要修改数据的Id
        int entityId=CommonUtility.isEmpty(request.getParameter("entityId"))?0:Integer.parseInt(request.getParameter("entityId"));
        ModelAndView model=new ModelAndView();
        CarModelOthername carModelOthername=new CarModelOthername();
        //查询所有的车型名称
        List<CarModel> CarModelList = carModelService.selectAll();
        // 进行数据准备
       // model.addObject("CarModelList",CarModelList);
        request.setAttribute("CarModelList", CarModelList);
        try {
            //判断是 修改  还是 新增
            if (!CommonUtility.isEmpty(entityId)&&entityId>0) {
                carModelOthername = carModelOthernameService.selectByPrimaryKey(entityId);
                // 进行数据准备
                model.addObject("carModelOthername",carModelOthername);
            }
            //返回视图位置
            model.setViewName("car/carModelOthername/carModelOthernameForm");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }

    /**
     * 方法说明：保存/修改
     * @author  ChenXH
     * @date  2018-10-29 16:54:27
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public String save (HttpServletRequest request, HttpServletResponse response,@RequestParam  Map<String,Object> jsonMap){
        //执行状态（0成功；1失败）
        int flag=0;
        int entityId=CommonUtility.isEmpty(jsonMap.get("entityId"))?0:Integer.parseInt(jsonMap.get("entityId")+"");
        //进行 修改
        if(entityId>0){
            CarModelOthername carModelOthername=carModelOthernameService.selectByPrimaryKey(entityId);
            if(!CommonUtility.isEmpty(carModelOthername)){

                try {
                    //修改
                    carModelOthernameService.updateByPrimaryKey(carModelOthername);
                    //operationLog(request, "修改用户" + user.getLoginName(), TableConstants.LOG_TYPES_EDIT);
                }catch (Exception e) {
                    flag = 1;
                    e.printStackTrace();
                }
            }else{
                flag=1;
            }
        }else{
            //进行数据添加
            CarModelOthername carModelOthername=new CarModelOthername();

            try {
                //新增
                carModelOthernameService.insert(carModelOthername);
                //operationLog(request, "添加用户" + user.getLoginName(), TableConstants.LOG_TYPES_ADD);
            }catch (Exception e) {
                flag = 1;
                e.printStackTrace();
            }
        }

        return flag+"";
    }

    /**
     * 方法说明：删除
     * @author  ChenXH
     * @date  2018-10-29 16:54:27
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public String delete (HttpServletRequest request, HttpServletResponse response){
        int entityId=CommonUtility.isEmpty(request.getParameter("entityId"))?0:Integer.parseInt(request.getParameter("entityId"));
        //执行状态（0成功；1失败）
        int flag = 0;
        if(entityId>0){
            CarModelOthername carModelOthername = carModelOthernameService.selectByPrimaryKey(entityId);
            if(!CommonUtility.isEmpty(carModelOthername)){
                //carModelOthername.setIsDel(0);
                try {
                	carModelOthernameService.deleteByPrimaryKey(carModelOthername);
                    //carModelOthernameService.updateByPrimaryKey(carModelOthername);
                    //operationLog(request, "删除用户" + user.getLoginName(), TableConstants.LOG_TYPES_DEL);
                }catch (Exception e) {
                    flag = 1;
                    e.printStackTrace();
                }
            }
        }
        return flag+"";
    }


}
